package controller;

import model.Product;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import service.ProductService;
import util.Pagination;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "product")
public class ProductController extends BaseController {
    private ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    //查询全部商品
    @GetMapping(value = "queryAll/{currentPage}")
    private String allProducts(@PathVariable("currentPage") int currentPage) {
        Pagination<Product> pagination = productService.queryAll(currentPage);
        session.setAttribute("pagination", pagination);
        return "redirect:/admin/productList.jsp";
    }
    @GetMapping(value = "queryAlls/{currentPage}")
    private String re_allProducts(@PathVariable("currentPage") int currentPage) {
        Pagination<Product> pagination = productService.queryAll(currentPage);
        session.setAttribute("pagination", pagination);
        return "redirect:/index.jsp";
    }
    //添加商品
    @PostMapping(value = "addProduct")
    private String addProduct(Product product, MultipartFile file) throws IOException {
        if (file.getSize() == 0){
            session.setAttribute("error","未选择文件！");
            return "redirect:/admin/addProduct/jsp";
        }

        File dir = new File(application.getRealPath("/static/images/product"));
        String contentType = file.getContentType();
        if (contentType != null && !contentType.startsWith("image")){
            session.setAttribute("error","选择的文件不是图片");
            return "redirect:/admin/addProduct/jsp";
        }
        String filename = UUID.randomUUID()+"."+ StringUtils.getFilenameExtension(file.getOriginalFilename());
        file.transferTo(new File(dir,filename));
        product.setPicture(filename);

        productService.create(product);
        return "redirect:/product/queryAll/1";
    }

    //删除商品
    @GetMapping(value = "removeProduct/{id}")
    private String removeProduct(@PathVariable("id") int id){
        productService.remove(id);
        Pagination<Product> pagination = productService.queryAll(1);
        session.setAttribute("pagination", pagination);
        return "redirect:/product/queryAll/1";
    }
    //商品详情
    @GetMapping(value = "detailProduct/{id}")
    private String detailProduct(@PathVariable("id") int id){
        session.setAttribute("product",productService.queryById(id));
        return "redirect:/detailProduct.jsp";
    }

    //编辑
    @GetMapping(value = "editer_queryById/{id}")
    private String editer_queryById(@PathVariable("id") int id){
        session.setAttribute("product",productService.queryById(id));
        return "redirect:/admin/updateProduct.jsp";
    }
    @PostMapping(value = "editer_update")
    private String editer_update(Product product,MultipartFile file) throws IOException {
        if (file.getSize() == 0){
            session.setAttribute("error","未选择文件！");
            return "redirect:/admin/addProduct/jsp";
        }

        File dir = new File(application.getRealPath("/static/images/product"));
        String contentType = file.getContentType();
        if (contentType != null && !contentType.startsWith("image")){
            session.setAttribute("error","选择的文件不是图片");
            return "redirect:/admin/addProduct/jsp";
        }
        String filename = UUID.randomUUID()+"."+ StringUtils.getFilenameExtension(file.getOriginalFilename());
        file.transferTo(new File(dir,filename));
        product.setPicture(filename);

        productService.modify(product);
        return "redirect:/product/queryAll/1";
    }
    //通过商品名称搜索商品
    @PostMapping(value = "researchProducts")
    private String researchProducts(Product product){
        List<Product> research_products = productService.queryList("queryProducts",product);
        if (!research_products.isEmpty()){
            session.setAttribute("flag",1);
        }else{
            session.setAttribute("flag",2);
        }
        session.setAttribute("research_products",research_products);
        return "redirect:/index.jsp";
    }
}
